package com.the4man.cnbdw.service.impl;

import com.the4man.cnbdw.dao.*;
import com.the4man.cnbdw.exception.BusinessException;
import com.the4man.cnbdw.service.MessageMobileService;
import com.the4man.framework.model.*;
import com.the4man.framework.utils.GenUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
@Service
public class MessageMobileServiceImpl implements MessageMobileService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private MessageReplyMapper messageReplyMapper;
    @Autowired
    private MessageRecordMapper messageRecordMapper;

    public MessageMobileServiceImpl() {
    }

    public int getMsgCnt(String empId, String type) throws BusinessException, Exception {
        MessageRecordExample example = new MessageRecordExample();
        MessageRecordExample.Criteria criteria = example.createCriteria();
        criteria.andNoOffDate();
        criteria.andRecUserEqualTo(empId);
        if(StringUtils.isNotEmpty(type)) {
            criteria.andIsReadEqualTo(type);
        }

        int count = this.messageRecordMapper.countByExample(example);
        return count;
    }

    public List<Message> getMsgPage(String empId, String type, int startRows, int pageSize) throws BusinessException, Exception {
        MessageExample example = new MessageExample();
        MessageExample.Criteria criteria = example.createCriteria();
        criteria.andRecUserEq(empId);
        criteria.andNoOffDate();
        if(StringUtils.isNotEmpty(type)) {
            criteria.andIsReadEqualTo(type);
        }

        example.setOrderByClause("MSG_CREATE_TIME DESC");
        example.setStartRow(startRows);
        example.setPageSize(pageSize);
        example.setEmpId(empId);
        List<Message> list = this.messageMapper.selectByExample(example);
        return list;
    }

    public boolean replyMessage(String message, String empId, String msgId) throws BusinessException, Exception {
        MessageReply messageReply = new MessageReply();
        messageReply.setReplyId(GenUtil.genUUID());
        messageReply.setMsgId(msgId);
        messageReply.setReplyUser(empId);
        messageReply.setReplyMessag(message);
        messageReply.setReplyTime(new Date());
        int updateRows = this.messageReplyMapper.insert(messageReply);
        return updateRows > 0;
    }

    public boolean updateToIsRead(String empId, List<String> msgIdList) throws BusinessException, Exception {
        MessageRecordExample example = new MessageRecordExample();
        MessageRecord record = new MessageRecord();
        record.setIsRead("1");
        MessageRecordExample.Criteria criteria = example.createCriteria();
        criteria.andRecUserEqualTo(empId);
        criteria.andMsgIdIn(msgIdList);
        int rows = this.messageRecordMapper.updateByExampleSelective(record, example);
        return rows > 0;
    }
}
